package com.itheima.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static {
        try {
            //获取启动类加载器加载某一个资源文件
            InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties pt=new Properties();
            pt.load(in);

            driver = pt.getProperty("jdbc.driver");
            url=pt.getProperty("jdbc.url");
            username=pt.getProperty("jdbc.username");
            password=pt.getProperty("jdbc.password");
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        return  DriverManager.getConnection(url, username, password);
    }

    public static void close(ResultSet rs, Statement st,Connection cc){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(cc!=null){
            try {
                cc.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
